#!/bin/bash

# Scripts in the tools/ directory should source this file with the line:
# pushd "$(dirname $0)" >/dev/null && source common.sh && popd >/dev/null

export TOOLS_DIR=$(pwd)
export PROJECT_DIR=$(dirname $TOOLS_DIR)
export APP_DIR=$PROJECT_DIR/app
export LIB_DIR=$PROJECT_DIR/lib
export TESTS_DIR=$PROJECT_DIR/tests

for dir in \
    "$APPENGINE_DIR" \
    /usr/lib/google_appengine \
    /usr/local/lib/google_appengine \
    /usr/local/google_appengine \
    $HOME/google_appengine; do
    if [ -d "$dir" ]; then
        export APPENGINE_DIR="$dir"
    fi
done

if [ -z "$APPENGINE_DIR" ]; then
    echo "Could not find google_appengine directory.  Set APPENGINE_DIR."
    exit 1
fi

for dir in \
    "$SELENIUM_DIR" \
    /usr/lib/selenium \
    /usr/local/lib/selenium \
    /usr/local/selenium \
    $HOME/selenium; do
    if [ -d "$dir" ]; then
        export SELENIUM_DIR="$dir"
    fi
done

if [ -z "$APPENGINE_DIR" ]; then
    echo "Could not find selenium directory.  Set APPENGINE_DIR."
    exit 1
fi

export SELENIUM_JAR=$(find $SELENIUM_DIR -name selenium-server.jar)

if [ -z "$SELENIUM_JAR" ]; then
    echo "Could not find selenium-server.jar in $SELENIUM_DIR."
    exit 1
fi

export SELENIUM_PYTHON_DIR=$(dirname $(find $SELENIUM_DIR -name selenium.py))

if [ -z "$SELENIUM_PYTHON_DIR" ]; then
    #if [ $(python -m selenium) ]; then
    if python -m selenium; then
        export SELENIUM_PYTHON_DIR=""
    else
        echo "Could not find selenium.py in $SELENIUM_DIR nor egg in site-packages."
        exit 1
    fi
fi

for python in \
    $(which python2.5) \
    /usr/local/bin/python2.5 \
    /usr/bin/python2.5 \
    /Library/Frameworks/Python.framework/Versions/2.5/bin/python; do
    if [ -x "$python" ]; then
        export PYTHON="$python"
    fi
done

if [ -z "$PYTHON" ]; then
    DEFAULT_PYTHON="$(which python)"
    if [[ "$($DEFAULT_PYTHON -V 2>&1)" =~ "Python 2.5" ]]; then
        export PYTHON="$DEFAULT_PYTHON"
    fi
fi

if [ -z "$PYTHON" ]; then
    echo "Could not find python2.5 executable.  Set PYTHON."
    exit 1
fi

export PYTHONPATH=\
"$APP_DIR":\
"$LIB_DIR":\
"$TESTS_DIR":\
"$TOOLS_DIR":\
"$SELENIUM_PYTHON_DIR":\
"$APPENGINE_DIR":\
"$APPENGINE_DIR/lib/django":\
"$APPENGINE_DIR/lib/fancy_urllib":\
"$APPENGINE_DIR/lib/webob":\
"$APPENGINE_DIR/lib/yaml/lib"

function update_hgignore() {
    pushd $LIB_DIR >/dev/null
    sed -ne '1,/# ==== AUTOGENERATED/p' ../.hgignore > ../.hgignore.new
    for dir in *; do
        echo "app/$dir" >> ../.hgignore.new
        echo "feeddrop/$dir" >> ../.hgignore.new
    done
    cat ../.hgignore.new > ../.hgignore && rm ../.hgignore.new
    popd >/dev/null
}

function set_up_symlinks() {
    pushd $LIB_DIR >/dev/null
    find ../app ../feeddrop -type l -exec rm '{}' ';'
    for dir in *; do
        ln -s ../lib/$dir ../app/$dir || exit 1
        ln -s ../lib/$dir ../feeddrop/$dir || exit 1
    done
    popd >/dev/null
}
